from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from sys import argv
import sys

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

num1 = argv[1]
# num1 = 'A60J93K3RM6E1LYS'

try:
    driver = webdriver.Chrome(r'C:\Users\Administrator\Desktop\demo\auto_chrome_driver\chromedriver.exe',
                              chrome_options = chrome_options)  # 创建Chrome对象.

    # 操作这个对象.
    driver.get('https://www.chsi.com.cn/xlcx/bgcx.jsp')  # get方式访问
    driver.find_element_by_id('validationCode').send_keys(num1)  # 输入学籍验证码
    driver.find_element_by_id('submitForm').click()  # 点击验证按钮

    # 获取图片验证码的src属性
    src = ''
    try:
        src = driver.find_element_by_xpath('//*[@id="getXueLi"]/table/tbody/tr[1]/td[3]/img').get_attribute('src')
    except:
        pass
    if src is None or len(src) == 0:
        time.sleep(1)  # 睡眠1秒
        try:
            cnt = driver.find_element_by_xpath('//*[@id="fixedPart"]/table[2]/tbody/tr[1]/td[4]/div').text
        except Exception:
            print('验证结果：发现内鬼一名！')
            sys.exit(0)
        if cnt == '专科':
            print('验证结果：血统纯正！')
        else:
            print('验证结果：发现内鬼一名！')
    else:
        code = src.split('=')[1]  # 得到验证码
        driver.find_element_by_id('CHKNUM').send_keys(code)  # 输入验证码
        driver.find_element_by_name('Submit').click()  # 点击继续按钮
        time.sleep(1)  # 睡眠1秒
        try:
            cnt = driver.find_element_by_xpath('//*[@id="fixedPart"]/table[2]/tbody/tr[1]/td[4]/div').text
        except Exception:
            print('验证结果：发现内鬼一名！')
            sys.exit(0)
        if cnt == '专科':
            print('验证结果：血统纯正！')
        else:
            print('验证结果：发现内鬼一名！')
except Exception as e:
    if str(e).find('在线验证码不正确'):
        print('学籍验证码非法！')
        print(str(e))
    sys.exit(0)
